package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.SetMealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetMealService.class)
@Transactional
public class SetMealServiceImpl implements SetMealService{
    @Autowired
    private SetMealDao setMealDao;

    @Autowired
    private JedisPool jedisPool;

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setMealDao.add(setmeal);
        Integer setmealId = setmeal.getId();
        //将图片名称存入redis
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
        setMealDao.addSetMealIdAndCheckGroupId(setmealId,checkgroupIds);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();

        if(queryString!=null) {
            queryString="%"+queryString+"%";
        }
        PageHelper.startPage(currentPage,pageSize);

        Page<Setmeal>setmealPage=setMealDao.findPage(queryString);
        long total = setmealPage.getTotal();
        List<Setmeal> setmealList = setmealPage.getResult();
        return new PageResult(total,setmealList);
    }

    @Override
    public Setmeal findSetMealById(Integer id) {
        Setmeal setmeal=setMealDao.findSetMealById(id);
        return setmeal;
    }

    @Override
    public List<Integer> findSetMealAndCheckGroupById(Integer id) {
        List<Integer>integerList=setMealDao.findSetMealAndCheckGroupById(id);
        return integerList;
    }

    @Override
    public void update(Setmeal setmeal, Integer[] checkgroupIds) {
        setMealDao.update(setmeal);
        if(checkgroupIds!=null&&checkgroupIds.length>0){
            setMealDao.delSetMealAndCheckGroupById(setmeal.getId());
            setMealDao.addSetMealIdAndCheckGroupId(setmeal.getId(),checkgroupIds);
        }
    }

    @Override
    public void del(Integer id,String img ) {
        setMealDao.delSetMealAndCheckGroupById(id);
        setMealDao.del(id);
        String substring = img.substring(img.lastIndexOf("/") + 1);
        QiniuUtils.deleteFileFromQiniu(substring);
    }

    @Override
    public List<Setmeal> findSetMeal() {
        List<Setmeal> setMealList=setMealDao.findSetMeal();
        return setMealList;
    }

    @Override
    public Setmeal findAllSetMeal(Integer id) {
        Setmeal setmeal=setMealDao.findAllSetMeal(id);
        return setmeal;
    }

    @Override//setmealNames、setmealCount
    public Map getSetMealReport() {
        Map<String, Object> hashMap = new HashMap<>();
        //setmealCount查询获取所有名称name、数量value，
        List<Map> setmealCount=setMealDao.getSetMealReport();
        List<String> setmealNames = new ArrayList<>();
        for (Map map : setmealCount) {
            String name = (String) map.get("name");
            setmealNames.add(name);
        }
        hashMap.put("setmealNames",setmealNames);
        hashMap.put("setmealCount",setmealCount);

        return hashMap;
    }

    @Override
    public List<Map> getHotSetmeal() {
        List<Map> mapList=setMealDao.getHotSetmeal();
        return mapList;
    }
}
